import dayjs from 'dayjs';
Component({
  externalClasses: ['wr-class', 'header-class', 'title-class'],
  options: {
    multipleSlots: true,
  },

  relations: {
    '../order-goods-card/index': {
      type: 'descendant',
      linked(target) {
        this.children.push(target);
        this.setHidden();
      },
      unlinked(target) {
        this.children = this.children.filter((item) => item !== target);
      },
    },
    '../goods-card/index': {
      type: 'descendant',
      linked(target) {
        this.children.push(target);
        this.setHidden();
      },
      unlinked(target) {
        this.children = this.children.filter((item) => item !== target);
      },
    },
    '../specs-goods-card/index': {
      type: 'descendant',
      linked(target) {
        this.children.push(target);
        this.setHidden();
      },
      unlinked(target) {
        this.children = this.children.filter((item) => item !== target);
      },
    },
  },

  created() {
    this.children = [];
  },

  properties: {
    order: {
      type: Object,
      observer(order) {
        if (!order?.prices) return;
        order.prices = order?.prices.map((item) => {
          return {
            "id": item.id,
            "lineid": item.line_id,
            "start": item.start,
            "end": item.end,
            "price": item.price,
            "mprice": item.mprice,
            "time": dayjs(item.time).format("YYYY-MM-DD HH:mm")
          }
        });
        this.setData({
          order
        })
      },
    },
    useTopRightSlot: Boolean,
    //  初始显示的商品数量，超出部分会隐藏。
    defaultShowNum: {
      type: null,
      value: 10,
    },
    useLogoSlot: {
      type: Boolean,
      value: false,
    },
  },

  data: {
    showAll: true, // 是否展示所有商品，设置为false，可以使用展开更多功能
    goodsCount: 0,
    value: -1
  },

  methods: {
    setHidden() {
      const isHidden = !this.data.showAll;
      this.children.forEach(
        (c, i) => i >= this.properties.defaultShowNum && c.setHidden(isHidden),
      );
    },
    onChange1(e) {
      console.log(e.detail.value)
      this.triggerEvent('change', {
        train: this.properties?.order,
        value: e.detail.value
      });
    },
    onOrderCardTap() {
      this.triggerEvent('cardtap');
    },
    onChange(e) {
      if (this.data.value == e.detail.value) {
        this.setData({
          value: -1
        });
      } else {
        this.setData({
          value: e.detail.value
        });
      }

      if (this.data.value == -1) {
        this.triggerEvent('change', {
          value: NaN
        });
      } else {
        const order = this.properties?.order
        this.triggerEvent('change', {
          value: order.prices[e.detail.value]
        });
      }

    },
    onShowMoreTap() {
      this.setData({
        showAll: true
      }, () => this.setHidden());
      this.triggerEvent('showall');
    },
  },
});